package bulk_param.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;



public class VerificationMaxPointIDDao {
	
	private String userid = "arbor";
    private String password = "arbor123";
    private String pilote = "oracle.jdbc.driver.OracleDriver";
    private String urlcat = "jdbc:oracle:thin:@10.1.3.136:1522:cc_cat"; 
    private String urlcust = "jdbc:oracle:thin:@10.1.3.136:1522:cc_cust"; 
    private String url3p = "jdbc:oracle:thin:@10.1.3.136:1522:cust3p"; 
    private String urlnc = "jdbc:oracle:thin:@10.1.3.136:1522:custnc"; 
    
	private static Connection con;

    //constructor 
	public VerificationMaxPointIDDao(){
	}

	public Connection getConnectionCAT(){
				
		try {
			Class.forName(pilote);	

		} catch(java.lang.ClassNotFoundException e) {
			System.err.print("ClassNotFoundException: ");
			System.err.println(e.getMessage());
		}

		try {
			con = DriverManager.getConnection(urlcat, userid, password);
			System.out.println ("Ouverture Connection");     
		} catch(SQLException ex) {
			System.err.println("SQLException: " + ex.getMessage());
		}
		
		return con;
	}
	
	public Connection getConnectionCUST(){
		
		try {
			Class.forName(pilote);	

		} catch(java.lang.ClassNotFoundException e) {
			System.err.print("ClassNotFoundException: ");
			System.err.println(e.getMessage());
		}

		try {
			con = DriverManager.getConnection(urlcust, userid, password);
			System.out.println ("Ouverture Connection");     
		} catch(SQLException ex) {
			System.err.println("SQLException: " + ex.getMessage());
		}
		
		return con;
	}
	
	public Connection getConnection3P(){
		
		try {
			Class.forName(pilote);	

		} catch(java.lang.ClassNotFoundException e) {
			System.err.print("ClassNotFoundException: ");
			System.err.println(e.getMessage());
		}

		try {
			con = DriverManager.getConnection(url3p, userid, password);
			System.out.println ("Ouverture Connection");     
		} catch(SQLException ex) {
			System.err.println("SQLException: " + ex.getMessage());
		}
		
		return con;
	}
	
	public Connection getConnectionNC(){
		
		try {
			Class.forName(pilote);	

		} catch(java.lang.ClassNotFoundException e) {
			System.err.print("ClassNotFoundException: ");
			System.err.println(e.getMessage());
		}

		try {
			con = DriverManager.getConnection(urlnc, userid, password);
			System.out.println ("Ouverture Connection");     
		} catch(SQLException ex) {
			System.err.println("SQLException: " + ex.getMessage());
		}
		
		return con;
	}
	
	static  public  void  closeConnection(){         
		System.out.println ("Colse DBConnection.");          
		        
				
		try {
			con.close();             
			System.out.println ("Fermeture Connection");        
			}         
		catch (Exception ex) {             
			System.out.println ("ERROR :"+ex.getMessage());             
			ex.printStackTrace();        
			}    
		} 
	
	
	//Verification de l'existance du max point id
	public long maxPointIDCAT(){
		//Create Connection to the Oracle Database
		getConnectionCAT();
		long point_id=0;
		try{
			System.out.println("Verification max point_id CAT");
			String sql="select (Max(point_id)) from usage_points";
			
			// Create a prepared statement
 			Statement s = con.createStatement();

			ResultSet rs = s.executeQuery(sql);	
			
			while(rs.next()){ 			
				point_id=rs.getLong("(MAX(POINT_ID))");		
			}
		}
		catch(Exception e){
			System.out.println(e);
		}
		return point_id;
	}
	
	//Verification de l'existance du max point id
	public long maxPointIDCUST(){
		//Create Connection to the Oracle Database
		getConnectionCUST();
		long point_id=0;
		try{
			System.out.println("Verification max point_id CUST");
			String sql="select (Max(point_id)) from usage_points";
			
			// Create a prepared statement
 			Statement s = con.createStatement();

			ResultSet rs = s.executeQuery(sql);	
			
			while(rs.next()){ 			
				point_id=rs.getLong("(MAX(POINT_ID))");		
			}
		}
		catch(Exception e){
			System.out.println(e);
		}
		return point_id;
	}
		
	//Verification de l'existance du max point id
	public long maxPointID3P(){
		//Create Connection to the Oracle Database
		getConnection3P();
		long point_id=0;
		try{
			System.out.println("Verification max point_id CUST3P");
			String sql="select (Max(point_id)) from usage_points";
			
			// Create a prepared statement
 			Statement s = con.createStatement();

			ResultSet rs = s.executeQuery(sql);	
			
			while(rs.next()){ 			
				point_id=rs.getLong("(MAX(POINT_ID))");		
			}
		}
		catch(Exception e){
			System.out.println(e);
		}
		return point_id;
	}
		
	//Verification de l'existance du max point id
	public long maxPointIDNC(){
		//Create Connection to the Oracle Database
		getConnectionNC();
		long point_id=0;
		try{
			System.out.println("Verification max point_id CUSTNC");
			String sql="select (Max(point_id)) from usage_points";
			
			// Create a prepared statement
 			Statement s = con.createStatement();

			ResultSet rs = s.executeQuery(sql);	
			
			while(rs.next()){ 			
				point_id=rs.getLong("(MAX(POINT_ID))");		
			}
		}
		catch(Exception e){
			System.out.println(e);
		}
		return point_id;
	}
	
	public boolean isMaxPointIDEgaux(){
		long cust,cat,cust3p,custnc;
		boolean isEgaux=false ;
		
		cust=maxPointIDCUST();
		cat=maxPointIDCAT();
		cust3p=maxPointID3P();
		custnc=maxPointIDNC();
		
		if (cust==cat && cust ==cust3p && cust==custnc){
			isEgaux=true;
		}else{
			isEgaux=false;
		}
		
		return isEgaux;
	}
	
	
}
	
